package com.wx.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.dto.ContactsDto;
import com.wx.entity.Contacts;
import com.wx.service.ContactsService;
import com.wx.mapper.ContactsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 86158
* @description 针对表【contacts】的数据库操作Service实现
* @createDate 2024-10-20 16:00:56
*/
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts>
    implements ContactsService{

    @Autowired
    private ContactsMapper contactsMapper;
    @Override
    public SaResult queryContactsInfo() {
        List<Contacts> contactList= contactsMapper.selectList(null);
        return SaResult.get(200,"查询成功", contactList);


    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public SaResult addContacts(ContactsDto contactsDto) {
        Contacts contacts=new Contacts();
        contacts.setAddress(contactsDto.getAddress());
        contacts.setName(contactsDto.getName());
        contacts.setPhone(contactsDto.getPhone());
        contactsMapper.insert(contacts);
        return SaResult.ok("联系人添加成功！");
    }

    @Override
    public SaResult updateContacts(ContactsDto contactsDto,String id) {
        Contacts contacts=new Contacts();
        contacts.setAddress(contactsDto.getAddress());
        contacts.setName(contactsDto.getName());
        contacts.setPhone(contactsDto.getPhone());
        contacts.setId(id);
        contactsMapper.updateById(contacts);

        return SaResult.ok("联系人修改成功");
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public SaResult deleteContacts(String id) {
        // 首先检查用户是否存在
        Contacts contacts = contactsMapper.selectById(id);
        if (contacts == null) {
            return SaResult.error("联系人不存在，删除失败！");
        }
        // 删除用户
        contactsMapper.deleteById(id);
        return SaResult.ok("联系人删除成功！");
    }

}




